package com.adb.droid.ui;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.adb.droid.R;
import com.adb.droid.uc.QuickReturnListView;

public class FeedScreenActivity extends Activity{
	
	private static final String TAG = FeedScreenActivity.class
			.getCanonicalName();
	private TextView mlblFooterView;
	private QuickReturnListView mlstview_feeds_items;
	private int mQuickReturnHeight;
	
	private static final int STATE_ONSCREEN = 0;
	private static final int STATE_OFFSCREEN = 1;
	private static final int STATE_RETURNING = 2;
	private int mState = STATE_ONSCREEN;
	private int mScrollY;
	private int mMinRawY = 0;

	private TranslateAnimation anim;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG,"onCreate");
		setContentView(R.layout.activity_feed_screen);
		/** Array of feeds items **/
		String[] arrayFeedsItems = new String[] { "Item 1", "Item 2", "Item 3",
				"Item 4", "Item 5", "Item 6", "Item 7", "Item 8", "Item 9",
				"Item 10", "Item 11", "Item 12", "Item 13", "Item 14",
				"Item 15" };

		mlblFooterView = (TextView) findViewById(R.id.lblfooter);
		mlstview_feeds_items = (QuickReturnListView) findViewById(R.id.lstview_feeds_items);

		mlstview_feeds_items.setAdapter(new ArrayAdapter<String>(
				getApplicationContext(), R.layout.row_feed_item_list,
				R.id.lbl_feed_name, arrayFeedsItems));

		mlstview_feeds_items.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {
						// footer view height
						mQuickReturnHeight = mlblFooterView.getHeight();
						mlstview_feeds_items.computeScrollY();
					}
				});
		/**
		 * Set the listener that will receive notifications every time the list scrolls.
		 */
		mlstview_feeds_items.setOnScrollListener(new OnScrollListener() {
			@SuppressLint("NewApi")
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {

				mScrollY = 0;
				int translationY = 0;

				if (mlstview_feeds_items.scrollYIsComputed()) {
					mScrollY = mlstview_feeds_items.getComputedScrollY();
				}

				int rawY = mScrollY;

				switch (mState) {
				case STATE_OFFSCREEN:
					if (rawY >= mMinRawY) {
						mMinRawY = rawY;
					} else {
						mState = STATE_RETURNING;
					}
					translationY = rawY;
					break;

				case STATE_ONSCREEN:
					if (rawY > mQuickReturnHeight) {
						mState = STATE_OFFSCREEN;
						mMinRawY = rawY;
					}
					translationY = rawY;
					break;

				case STATE_RETURNING:

					translationY = (rawY - mMinRawY) + mQuickReturnHeight;

					System.out.println(translationY);
					if (translationY < 0) {
						translationY = 0;
						mMinRawY = rawY + mQuickReturnHeight;
					}

					if (rawY == 0) {
						mState = STATE_ONSCREEN;
						translationY = 0;
					}

					if (translationY > mQuickReturnHeight) {
						mState = STATE_OFFSCREEN;
						mMinRawY = rawY;
					}
					break;
				}

				/** this can be used if the build is below honeycomb **/
				if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB) {
					anim = new TranslateAnimation(0, 0, translationY,
							translationY);
					anim.setFillAfter(true);
					anim.setDuration(0);
					mlblFooterView.startAnimation(anim);
				} else {
					mlblFooterView.setTranslationY(translationY);
				}

			}

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}
		});
		
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG,"onStart");
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG,"onStop");
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG,"onDestroy");
	}
}
